package org.loong.crypto.service.core.provider.impl;

import java.util.Collections;
import java.util.Set;

import org.loong.crypto.core.algorithm.CipherAlgorithm;
import org.loong.crypto.core.algorithm.EncryptionMethod;
import org.loong.crypto.service.core.context.CryptoContext;
import org.loong.crypto.service.core.provider.CipherProvider;

/**
 * The base abstract class for cipher (encryption) provider.
 */
public abstract class BaseCipherProvider implements CipherProvider {

    /**
     * The supported algorithms by the cipher (encryption) provider instance.
     */
    private final Set<CipherAlgorithm> algs;

    /**
     * The supported encryption methods by the JWE provider instance.
     */
    private final Set<EncryptionMethod> encs;

    /**
     * The cryptoContext.
     */
    private CryptoContext cryptoContext;
    
    /**
     * Creates a new base cipher (encryption) provider.
     *
     * @param algs The supported algorithms by the cipher (encryption) provider instance. Must not be {@code null}.
     * @param encs The supported encryption methods by the cipher (encryption) provider instance. Must not be
     *            {@code null}.
     */
    public BaseCipherProvider(final Set<CipherAlgorithm> algs, final Set<EncryptionMethod> encs) {
        if (algs == null) {
            throw new IllegalArgumentException("The supported cipher (encryption) algorithm set must not be null");
        }
        this.algs = Collections.unmodifiableSet(algs);

        if (encs == null) {
            throw new IllegalArgumentException("The supported cipher (encryption) methods set must not be null");
        }
        this.encs = encs;
    }

    @Override
    public Set<CipherAlgorithm> supportedCipherAlgorithms() {
        return algs;
    }
    
    @Override
    public Set<EncryptionMethod> supportedEncryptionMethods() {
        return encs;
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T extends CryptoContext> T getCryptoContext() {
        return (T) cryptoContext;
    }

    @Override
    public void setCryptoContext(CryptoContext cryptoContext) {
        this.cryptoContext = cryptoContext;
    }
}
